﻿using System;
using System.ComponentModel;
using DataStore;
using DataStore.Domain;
using DataStore.Repositories;
using Infrastructure.CurrentData;
using Microsoft.Practices.Composite.Events;

namespace Infrastructure.ViewModels
{
    /// <summary>
    /// Link viewmodel
    /// </summary>
    public class LinkViewModel : DataModelSingle<Link>, IDataErrorInfo
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LinkViewModel"/> class.
        /// </summary>
        /// <param name="link">The link.</param>
        public LinkViewModel(Link link) : base(link)
        {
            Initialize(link);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LinkViewModel"/> class.
        /// </summary>
        /// <param name="eventAggregator">The event aggregator.</param>
        /// <param name="link">The link.</param>
        public LinkViewModel(IEventAggregator eventAggregator, Link link) : base(eventAggregator, link)
        {
            Initialize(link);
        }

        /// <summary>
        /// Initializes the specified link.
        /// </summary>
        /// <param name="link">The link.</param>
        void Initialize(Link link)
        {
            
            State = ModelState.Active;
        }

        /// <summary>
        /// Gets the current link.
        /// </summary>
        /// <value>The link.</value>
        public Link Link
        {
            get
            {
                return Item;
            }
        }


        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        /// <value>The id.</value>
        public virtual Guid Id
        {
            get
            {
                return Item.Id;
            }
            set
            {
                if (Item.Id != value)
                {
                    Item.Id = value;
                    OnPropertyChanged("Id");
                }
            }
        }
        /// <summary>
        /// Gets or sets the URI.
        /// </summary>
        /// <value>The URI.</value>
        public virtual Uri Uri
        {
            get
            {
                return Item.Uri;
            }
            set
            {
                if (Item.Uri != value)
                {
                    Item.Uri = value;
                    OnPropertyChanged("Uri");
                }
            }
        }
        /// <summary>
        /// Gets or sets the description.
        /// </summary>
        /// <value>The description.</value>
        public virtual string Description
        {
            get
            {
                return Item.Description;
            }
            set
            {
                if (Item.Description != value)
                {
                    Item.Description = value;
                    OnPropertyChanged("Description");
                }
            }
        }
        /// <summary>
        /// Gets or sets the location.
        /// </summary>
        /// <value>The location.</value>
        public virtual LinkType Location
        {
            get
            {
                return Item.Location;
            }
            set
            {
                if (Item.Location != value)
                {
                    Item.Location = value;
                    OnPropertyChanged("Location");
                }
            }
        }

        /// <summary>
        /// Gets a value indicating whether this link is new.
        /// </summary>
        /// <value><c>true</c> if this link is new; otherwise, <c>false</c>.</value>
        protected override bool IsNewItem
        {
            get
            {
                return Item.Id == Guid.Empty;
            }
        }

        /// <summary>
        /// Saves this link.
        /// </summary>
        public override void Save()
        {
            IDatabase database = Current.Container.Resolve<IDatabase>();
            using (var unitOfWork = database.CreateUnitOfWork())
            {

                try
                {
                    IRepository<Link> repository = new LinkRepository(unitOfWork);
                    unitOfWork.BeginTransaction();

                    repository.SaveOrUpdate(Item);

                    unitOfWork.CommitTransaction();
                }
                catch (Exception)
                {
                    unitOfWork.RollbackTransaction();
                    throw;
                }
            }


            base.Save();
        }

        /// <summary>
        /// Gets a value indicating whether this link can be saved.
        /// </summary>
        /// <value><c>true</c> if this link can saved; otherwise, <c>false</c>.</value>
        protected override bool CanSave
        {
            get
            {
                return true;
            }

        }


        /// <summary>
        /// Gets the <see cref="System.String"/> with the specified column name.
        /// </summary>
        /// <value></value>
        public string this[string columnName]
        {
            get { throw new NotImplementedException(); }
        }

        /// <summary>
        /// Gets an error message indicating what is wrong with this object.
        /// </summary>
        /// <value></value>
        /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
        public string Error
        {
            get { throw new NotImplementedException(); }
        }
    }
}